package com.lzb.compressmaster.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.databinding.ActivityVideoPreviewBinding;
import com.lzb.compressmaster.utils.MediaUtil;
import com.lzb.compressmaster.utils.TimeUtil;

public class VideoPreviewActivity extends BaseAc<ActivityVideoPreviewBinding> implements View.OnClickListener {

    public static String sVideoPath;
    private long videoLength;

    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.sbPlay.setProgress(mDataBinding.videoView.getCurrentPosition() / 1000);
            mDataBinding.tvPlayLeft.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_video_preview;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoPreviewBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivPlay.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        if (id==R.id.ivPlay){//播放/暂停
            if (mDataBinding.videoView.isPlaying()) {
                mDataBinding.ivPlay.setImageResource(R.drawable.abf2);
                mDataBinding.videoView.pause();
                stopTime();
            } else {
                mDataBinding.ivPlay.setImageResource(R.drawable.azt2);
                mDataBinding.videoView.start();
                startTime();
            }
        }
    }

    @Override
    protected void initData() {
        setPlayer();
    }

    private void setPlayer() {
        mHandler = new Handler();
        videoLength = MediaUtil.getDuration(sVideoPath);
        mDataBinding.sbPlay.setMax((int) (videoLength / 1000));
        mDataBinding.tvPlayRight.setText(TimeUtil.getMmss(videoLength));
        mDataBinding.videoView.setVideoPath(sVideoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.ivPlay.setImageResource(R.drawable.abf2);
                mDataBinding.sbPlay.setProgress(0);
                mDataBinding.videoView.seekTo(1);
                mDataBinding.tvPlayLeft.setText("00:00");
                stopTime();
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlay.setImageResource(R.drawable.azt2);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDataBinding.ivPlay.setImageResource(R.drawable.abf2);
        mDataBinding.videoView.pause();
        stopTime();
    }
}